<?php

namespace App\Http\Controllers\Wechat;

use App\Libraries\classs\PhpRedis;
use App\Models\WxUser;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Wechat\BaseController;
use Redirect, Input, Response;
use App\Models\User\Build\Logs as BuildLogs;
use App\Models\User\Prize\Logs as Logs;
use App\Libraries\classs\SmsLib;
use App\Libraries\classs\red\Send;
use App\Models\Shares\Red\Logs as Red;

class MineController extends BaseController
{
    public function index(){

        $data['BuildLog'] = BuildLogs::with('build')->where('x_uid','=',$this->_uid)->take(5)->orderBy('updated_at','desc')->get();
        $data['prize_logs'] = Logs::with(array('prize'=>function($query){
            $query->with(array('attach' => function ($query) {
                $query->select('id','path');
            }))->select('id','name','info','attach_id','status');
        }))->where('x_uid','=',$this->_uid)->where('status','!=',-1)->orderBy('id','desc')->get();
        return view("wechat.mine.index",$data);
    }
    public function award(){
        return view('wechat.mine.award');
    }

    public function face(){
    }

    public function build(){

    }

    public function exchange(Request $request,$id){
        if(empty($this->_user->phone) && isset($this->_user->username)){
            return redirect('/wechat/mine/info/'.$id);
        }
       $prize_log =  Logs::with(array('prize'=>function($query){
            $query->with(array('attachBo' => function ($query) {
                $query->select('id', 'path');
            }))->select('id','name','info','attach_id');
        }))->find($id);
       return view('wechat.mine.exchange',array('prize'=>$prize_log));
    }

    public function verifyCode(Request $request){
        if($request->ajax()){
            $l_id =  $request->get('l_id');
            $v_code = $request->get('v_code');
            if($l_id && $v_code){
                 $prize = Logs::with(array('prize'=>function($query){
                     $query->select('id','passwd','cash','name');
                 }))->find($l_id);
                if($prize){
                    if($prize->status == 1){
                        if($prize->prize->passwd == $v_code){
                            if($prize->prize->cash){
                                $luckyMoneyData = [
                                    'send_name'        => $prize->prize->name,
                                    're_openid'        => $this->_user->openid,
                                    'total_num'        => 1,  //固定为1，可不传
                                    'total_amount'     => $prize->prize->cash,  //单位为分，不小于300
                                    'wishing'          => $prize->prize->name,
//                                  'client_ip'        => '192.168.0.1',  //可不传，不传则由 SDK 取当前客户端 IP
                                    'act_name'         => $prize->prize->name,
                                    'remark'           => $prize->prize->name,
                                ];
                                $rs = Send::getInstance()->sendNormal($luckyMoneyData);
                                if($rs['result_code'] == "SUCCESS") {
                                    $red_data = array(
                                        'x_uid' => $this->_uid,
                                        'red' => $prize->prize->cash,
                                        'type' => 2,
                                    );
                                    Red::create($red_data);
                                }
                            }
                            $prize->status =2;
                            $prize->save();
                            return Response::json(array('success'=>true,'msg'=>'兑换成功!'));
                        }
                        return Response::json(array('success'=>false,'msg'=>'验证码错误!'));
                    }elseif ($prize->status == -1){
                        return Response::json(array('success'=>false,'msg'=>'你还没有抽到奖品!,赶紧去刷脸投票,赢取大奖吧!'));
                    }
                    return Response::json(array('success'=>false,'msg'=>'您已经兑换过!请不要重复兑换!'));
                }
                return Response::json(array('success'=>false,'msg'=>'数据不存在!'));
            }
            return Response::json(array('success'=>false,'msg'=>'参数不能为空'));
        }
        return Response::json(array('success'=>false,'msg'=>'请求错误'));
    }

    public function info($id){
        return view("wechat.mine.info",array('id'=>$id));
    }

    public function  submit(Request $request){
        if($request->ajax()){
            $username = $request->get('username');
            $phone = $request->get("phone");
            $code = $request->get('code');
            if(!$code){
                return Response::json(array('success'=>false,'msg'=>'短信验证码不能为空!'));
            }
            if($username && $phone){
                 if($this->_user->username &&  $this->_user->phone){
                     return Response::json(array('success'=>true,'msg'=>'您已经提交过数据!'));
                 }
                $verify =SmsLib::VerifyCode($phone,$code,$this->_uid);
                if($verify['success'] == true){
                    if(PhpRedis::getInstance()->updateRedisUser($this->_uid,$data=array('username'=>$username,'phone'=>$phone))){
                        return Response::json(array('success'=>true,'msg'=>'提交成功!'));
                    }
                    return Response::json($verify);
                }
                return Response::json(array('success'=>false,'msg'=>'提交错误!'));
            }
            return Response::json(array('success'=>false,'msg'=>'手机号或用户名不能为空!'));
        }
        return Response::json(array('success'=>false,'msg'=>'请求错误'));
    }

    public function sendSms(Request $request){
        if($request->isMethod("POST")){
            $phone = $request->get("phone");
            if(!$phone){
                return Response::json(array('success'=>false,'msg'=>'手机号码不能为空'));
            }
            $rs = SmsLib::sendSms($phone,$this->_uid);
            if($rs['success'] == true){
                return Response::json(array('success'=>true));
            }
            return Response::json($rs);
        }
        return Response::json(array('success'=>false,'msg'=>'请求错误'));
    }
}
